package me.jessyan.armscomponent.app.mvp.ui.adapter;

import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;


import com.alibaba.android.arouter.launcher.ARouter;

import java.util.ArrayList;

import me.jessyan.armscomponent.commonsdk.core.RouterHub;


/**
 * 主页面 Fragment 适配器
 */
public class MainViewPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments = new ArrayList<>();
    private Fragment currentFragment;

    public MainViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        fragments.clear();
        fragments.add(getLearningHomeFragment());
        fragments.add(getExploreHomeFragment());
        fragments.add(getDonationHomeFragment());
        fragments.add(getAssetsHomeFragment());
        fragments.add(getMineHomeFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            currentFragment = ((Fragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }

    /**
     * Get the current fragment
     */
    public Fragment getCurrentFragment() {
        return currentFragment;
    }



    public static Fragment getLearningHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.LEARNING_HOMEFRAGMENT).navigation();
        return fragment;
    }
    public static Fragment getExploreHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.EXPLORE_HOMEFRAGMENT).navigation();
        return fragment;
    }

    public static Fragment getDonationHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.DONATION_HOMEFRAGMENT).navigation();
        return fragment;
    }

    public static Fragment getAssetsHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.ASSETS_HOMEFRAGMENT).navigation();
        return fragment;
    }

    public static Fragment getMineHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.MINE_HOMEFRAGMENT).navigation();
        return fragment;
    }

}